from conn_mysql import MysqlApi

class LoginNotify:

    def __init__(self, user, pass_word, method):

        self.user = user
        self.pass_word = pass_word
        self.method = method
        self.method_notify = self.method_notify()
        self.login_notify = self.login_notify()

    def method_notify(self):

        if self.method == 'POST':

            return {

                "status": 1,
                "errorCode": None,
                "errorMsg": None,
                "resultBody": "验证通过"          
            }

        else:

            return {

                "status": -1,
                "errorCode": 2,
                "errorMsg": "请检查你的请求方式，应为POST",
                "resultBody": None          
            }


    def login_notify(self):#登录检测函数

        if self.method_notify['status'] == -1:#如果请求方式不合法

            return self.method_notify

        with MysqlApi() as MA:

            select_result = MA.select_user(self.user, self.pass_word)

        if select_result:#有结果代表账号和密码匹配

            select_result.pop('pass_word')#将密码从结果中去除

            return {

                "status": 1,
                "errorCode": None,
                "errorMsg": None,
                "resultBody": {'user_info': select_result}          
            }

        else:

            return {

                "status": -1,
                "errorCode": 1,
                "errorMsg": f"请检查你的参数,user:{self.user},pass_word:{self.pass_word}",
                "resultBody": None          
            }

    